import { BASE_API } from './index.js'

module.exports = (vm) => {
	uni.$u.http.setConfig((config) => {
		config.baseURL = BASE_API
		return config
	})
	
	uni.showLoading();

	// 请求拦截
	uni.$u.http.interceptors.request.use((config) => {
		const token = uni.getStorageSync('aiToken')
		uni.hideLoading()
		config.header = {
			...config.header,
			Authorization: `bearer ${token}`
		}
		return config
	}, config => {
		uni.hideLoading()
		return Promise.reject(config)
	})

	// 响应拦截
	uni.$u.http.interceptors.response.use((response) => {
		const data = response.data
		return data.data === undefined ? data : data.data
	}, (response) => {
		return Promise.reject(response)
	})
}
